<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
    <#include "/common/elibs.html">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
</head>
<script>var index;
</script><body>
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" name="idxProAdmin" id="idxProAdmin" value="${id}"/>
        <input type="hidden" name="idProPowerSupplyScheme" id="idProPowerSupplyScheme" value="${subsetId}"/>
        <input type="hidden" name="paramsType" id="paramsType" value="${type}"/>

        <div class="layui-form-item">
            <label class="layui-form-label">
                开始时间
            </label>
            <div class="layui-input-inline">
                <input type="text" id="beginTime" name="beginTime"
                       autocomplete="off" class="layui-input" disabled style="background-color: #b7b1b1;">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                结束时间
            </label>
            <div class="layui-input-inline">
                <input type="text" id="endTime" name="endTime"
                       autocomplete="off" class="layui-input" disabled style="background-color: #b7b1b1;">
            </div>
        </div>

        <div class="layui-form-item" id="difference">
            <label class="layui-form-label">
                距离结束时间天数
            </label>
            <div class="layui-input-inline">
                <input type="text" id="differenceTime" name="differenceTime"
                       autocomplete="off" class="layui-input" disabled style="background-color: #b7b1b1;">
            </div>
        </div>

        <div class="layui-form-item" id="electricityPrice" style="padding: 20px; background-color: #F2F2F2;">

        </div>





        <div class="layui-form-item">
            <label for="" class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" type=button codeTyle="3" lay-submit="">
                提交
            </button>
        </div>
    </form>
</div>
<script>
    var countDigit = 1;

    function powerSupply(){
        var html = '<div class="layui-col-md6 powerSupply" style="width: 100%;">\n' +
            '                    <div class="layui-card">\n' +
            '                        <div class="layui-card-header">执行电价信息</div>\n' +
            '                        <div class="layui-form-item">\n' +
            '                            <label for="" class="layui-form-label">\n' +
            '                                <span class="x-red">*</span>执行电价\n' +
            '                            </label>\n' +
            '                            <div class="layui-input-inline">\n' +
            '                                根据用电类别、供电电压、手电容量，确定你的单位第一计量点执行<br/>\n' +
            '<input type="hidden" class="electricityPriceOne" value>' +
            '                                <input type="radio" name="electricityPriceOne'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceOne"  value="1" title="设备容量">\n' +
            '                                <input type="radio" name="electricityPriceOne'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceOne"  value="2" title="最大需量计收">\n' +
            '                                <br/>电价，\n' +
            '                                第二计量点执行<br/>\n' +
            '<input type="hidden" class="electricityPriceTwo" value>' +
            '                                <input type="radio" name="electricityPriceTwo'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceTwo"  value="1" title="设备容量">\n' +
            '                                <input type="radio" name="electricityPriceTwo'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceTwo"  value="2" title="最大需量计收">\n' +
            '                                <br/>电价\n' +
            '                            </div>\n' +
            '                            <div class="layui-form-mid layui-word-aux">\n' +
            '                                <span class="x-red">*</span>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '\n' +
            '                        <div class="layui-form-item">\n' +
            '                            <label for="" class="layui-form-label">\n' +
            '                                <span class="x-red">*</span>功率因数考核标准值\n' +
            '                            </label>\n' +
            '                            <div class="layui-input-inline">\n' +
            '                                根据国家税点财字【83】第215号《功率因数调整电费管理办法》规定，你的单位功率因数调整电费的考核标准为\n' +
            '<input type="hidden" class="electricityPriceStandard" value>' +
            '                                <br/><input type="radio" name="electricityPriceStandard'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceStandard"  value="1" title="0.8">\n' +
            '                                <input type="radio" name="electricityPriceStandard'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceStandard"  value="2" title="0.85">\n' +
            '                                <input type="radio" name="electricityPriceStandard'+countDigit+'"\n' +
            '                                       lay-verify="otherReq" lay-filter="electricityPriceStandard"  value="3" title="0.9">\n' +
            '                            </div>\n' +
            '                            <div class="layui-form-mid layui-word-aux">\n' +
            '                                <span class="x-red">*</span>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                        <div class="layui-form-item">\n' +
            '                            <div class="layui-btn" type=button onclick="powerSupply()">添加电源信息</div>\n' +
            '                            <div class="layui-btn" type=button onclick="deleteTemp(this)">删除当前电源信息</div>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </div>';
        $("#electricityPrice").append(html);
        renderForm();
        countDigit++;
    }


    function deleteTemp(o){
        $(o).parent().parent().parent().remove();
    }

    $(function(){
        getProTime($("#idxProAdmin").val(),20);
        var type = $("#paramsType").val();
        if(type === 'edit'){
            ajaxFunJson('POST','/pc/project/getPowerSupplySchemeInfo',JSON.stringify({"idProAdmin":$("#idxProAdmin").val()}),function(data){
                if(data.code != 0){
                    alertFun(data);
                }else{
                    var proPowerSupplySchemeElectricityPriceList = data.data.proPowerSupplySchemeElectricityPriceList;
                    var j=0;
                    for(var i=0;i<proPowerSupplySchemeElectricityPriceList.length;i++){
                        console.log(proPowerSupplySchemeElectricityPriceList[i]);
                        powerSupply();
                        if(isNotNull(proPowerSupplySchemeElectricityPriceList[i])){
                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceOne)){
                                $(".layui-card").children(".layui-form-item").children(".layui-input-inline")
                                    .children("input[name=electricityPriceOne"+(i+1)+"][value="+proPowerSupplySchemeElectricityPriceList[i].electricityPriceOne+"]").prop("checked","true");
                            }
                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceTwo)){
                                $(".layui-card").children(".layui-form-item").children(".layui-input-inline")
                                    .children("input[name=electricityPriceTwo"+(i+1)+"][value="+proPowerSupplySchemeElectricityPriceList[i].electricityPriceTwo+"]").prop("checked","true");
                            }

                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceStandard)){
                                $(".layui-card").children(".layui-form-item").children(".layui-input-inline")
                                    .children("input[name=electricityPriceStandard"+(i+1)+"][value="+proPowerSupplySchemeElectricityPriceList[i].electricityPriceStandard+"]").prop("checked","true");
                            }

                            renderForm();
                            j++;
                        }

                    }

                    j=0;
                    $(".layui-card").each(function(){
                        if(j > -1 && j < proPowerSupplySchemeElectricityPriceList.length){
                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceOne)) {
                                $(this).children(".layui-form-item").children(".layui-input-inline")
                                    .children(".electricityPriceOne").val(proPowerSupplySchemeElectricityPriceList[j].electricityPriceOne);
                            }
                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceTwo)) {
                                $(this).children(".layui-form-item").children(".layui-input-inline")
                                    .children(".electricityPriceTwo").val(proPowerSupplySchemeElectricityPriceList[j].electricityPriceTwo);
                            }
                            if(isNotNull(proPowerSupplySchemeElectricityPriceList[i].electricityPriceStandard)) {
                                $(this).children(".layui-form-item").children(".layui-input-inline")
                                    .children(".electricityPriceStandard").val(proPowerSupplySchemeElectricityPriceList[j].electricityPriceStandard);
                            }

                        }
                        j++;
                    })
                }
            })
        }else if(type === 'add'){
            powerSupply();
        }
    })

    layui.use(['form','layer'], function() {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer, layedit = layui.layedit;

        //自定义验证规则
        form.verify({
            otherReq: function(value,item){
                var $ = layui.$;
                var verifyName=$(item).attr('name')
                    , verifyType=$(item).attr('type')
                    ,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
                    ,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
                    ,isTrue= verifyElem.is(':checked')//是否命中校验
                    ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
                if(!isTrue || !value) {
                    //定位焦点
                    focusElem.css(verifyType == 'radio' ? {"color": "#FF5722"} : {"border-color": "#FF5722"});
                    //对非输入框设置焦点
                    focusElem.first().attr("tabIndex", "1").css("outline", "0").blur(function () {
                        focusElem.css(verifyType == 'radio' ? {"color": ""} : {"border-color": ""});
                    }).focus();
                    return '必填项不能为空';
                }
            }
        });


        //添加电源信息
        layui.$('#addPowerSupply').on('click', function(){
            powerSupply();
        });

        for(var q=1;q<countDigit;q++){

        }

        form.on('radio(electricityPriceOne)', function (data) {
            $(this).siblings(".electricityPriceOne").val(data.value);
        })
        form.on('radio(electricityPriceTwo)', function (data) {
            $(this).siblings(".electricityPriceTwo").val(data.value);
        })
        form.on('radio(electricityPriceStandard)', function (data) {
            $(this).siblings(".electricityPriceStandard").val(data.value);
        })

        //监听提交
        form.on('submit(add)', function (t) {
            addSubmit(3);
        });


    });

    function addSubmit(submitState){
        var myArray=new Array(),i = 0;
        $(".layui-card").each(function(){
            var data = {
                "electricityPriceOne":$(this).children(".layui-form-item").children(".layui-input-inline")
                    .children(".electricityPriceOne").val(),
                "electricityPriceTwo" : $(this).children(".layui-form-item").children(".layui-input-inline")
                    .children(".electricityPriceTwo").val(),
                "electricityPriceStandard" : $(this).children(".layui-form-item").children(".layui-input-inline")
                    .children(".electricityPriceStandard").val()
            }
            myArray[i] = data;
            i++
        })
        var params = {
            //tcaDescribe : layui.layedit.getContent(index),
            "idProAdmin": $("#idxProAdmin").val(),
            "idProPowerSupplyScheme":$("#idProPowerSupplyScheme").val(),
            "proPowerSupplySchemeElectricityPriceList":myArray,
        }
        console.log(params);
        debugger
        if (true) {
            ajaxFunJson('post','/pc/project/updatePowerSupplyScheme',JSON.stringify(params),function(data){
                if (data.code != 0) {
                    alertFun(data);
                } else {
                    window.location.href = path + "/modules/project/onePro/zdgdfa/list.html";
                    console.log("success !");
                }
            })
            return false;
        }
    }
</script></body></html>